package com.hongxun.design.utils.fileutil;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.bean.BackPhoto_Bean;
import com.hongxun.design.bean.BackPhotosBean;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.bean.PhotoPassagewayItemBean;
import com.hongxun.design.bean.PointSumBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.SearchProjectBean;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.StringUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.projectdatautil.DataFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/8/19.
 */
public class GetFileUtil {

    /**
     * 得到项目列表数据*
     */
    public static List<ProjectItemBean> getProjectStageListFile() {
        String json = MyApp.File.ReadFileData(MString.ProjectStageListFile + addEnd());//获取缓存项目列表
        List<ProjectItemBean> Data = new ArrayList<>();
        if (check(json)) {
            Data.addAll(FastJsonUtil.JsonToGetProjectListBean(json));
        }
        List<ProjectItemBean> list = new ArrayList<>();
        for (ProjectItemBean b : Data) {
            if (b != null && b.getCost_center() != null && b.getCost_center().equalsIgnoreCase(MyApp.userInfo.getOrg_id())) {
                list.add(b);
            }
        }

        return list;
    }
 /**
     * 得到项目列表离线数据*
     */
    public static List<ProjectItemBean> getProjectStageListOffLineFile() {
        String json = MyApp.File.ReadFileData(MString.ProjectStageListFile + addEnd());//获取缓存项目列表
        List<ProjectItemBean> Data = new ArrayList<>();
        if (check(json)) {
            Data.addAll(FastJsonUtil.JsonToGetProjectListBean(json));
        }
        List<ProjectItemBean> list = new ArrayList<>();
        for (ProjectItemBean b : Data) {
            if (b != null && b.getCost_center() != null && b.getCost_center().equalsIgnoreCase(MyApp.userInfo.getOrg_id())
                    &&isIdNull(b.getStage_id())&&!isIdNull(b.getUUID())
                    ) {
                list.add(b);
            }
        }

        return list;
    }

    private static boolean  isIdNull(String id){
        return StringUtil.isEmpty(id) || id.equals("0");
    }

    /**
     * 得到项目地图数据*
     */
    public static List<SearchProjectBean> getMapProjectFile() {
        String json = MyApp.File.ReadFileData(MString.ProjectListFile + addEnd());
        List<SearchProjectBean> Data = new ArrayList<>();
        if (check(json)) {
            Data.addAll(FastJsonUtil.JsonToGetSearchAllProject(json));
        }
        List<SearchProjectBean> list = new ArrayList<>();
        for (SearchProjectBean b : Data) {
            if (b.getCost_center().equalsIgnoreCase(MyApp.userInfo.getOrg_id())) {
                list.add(b);
            }
        }

        return list;
    }

    /**
     * 得到缓存集合
     **/
    public static List<ProjectItemBean> getProjectStageListCacheFile() {
        List<ProjectItemBean> Data = GetFileUtil.getProjectStageListFile();//MyApp.File.ReadFileData(MString.ProjectStageListFile + addEnd());//获取缓存项目列表
//        List<ProjectItemBean> Data = new ArrayList<>();
//            Data.addAll(FastJsonUtil.JsonToGetProjectListBean(json));

//        List<ProjectItemBean> list = new ArrayList<>();
//        if (Data==null)Data=new ArrayList<>();
//        for (ProjectItemBean b : Data) {
//            if (b.getCost_center().equalsIgnoreCase(MyApp.userInfo.getOrg_id())) {
//                list.add(b);
//            }
//        }
        return getList(Data);
    }

    /**
     * 过滤不存在的条目
     **/
    private static List<ProjectItemBean> getList(List<ProjectItemBean> list) {
        if (list == null || list.size() == 0) return new ArrayList<>();
        List<ProjectItemBean> Data = new ArrayList<>();
        for (ProjectItemBean b : list) {
            if (DataFactory.check_rcache(MyApp.activity, MString.Project_ + (StringUtil.isEmpty(b.getUUID())?b.getStage_id():b.getUUID()))||DataFactory.check_rcache(MyApp.activity, MString.Project_ + b.getStage_id())) {
                Data.add(b);
            }
        }
        return Data;
    }

    /**
     * 项目1级界面获取统计数据*
     */
    public static PointSumBean getPointSum() {
        String json = MyApp.File.ReadFileData(addHeadPath() + MString.ProjectStageTotalInfo + "_" + SetFileUtil.CheckStageId() + addEnd());

        if (check(json)) {
            return FastJsonUtil.JsonToGetPointSum(json);
        }
        return null;
    }

    /**
     * 项目1级界面台区数据*
     */
    public static List<Exploration> getTransformerLis() {
        String json = MyApp.File.ReadFileData(addHeadPath() + MString.ProjectTransformerList + "_" + SetFileUtil.CheckStageId() + addEnd());
        List<Exploration> Data = new ArrayList<>();
        if (check(json)) {
            Data.addAll(FastJsonUtil.JsonToGetExplorationList(json));
        }
        return Data;
    }


    /**
     * 项目踏勘点数据（新建与拆旧）*
     */
    public static List<Exploration> getExplorationList(String type) {
        String json = MyApp.File.ReadFileData(addHeadPath() + MString.ProjectStageExplorePointList + "_" + SetFileUtil.CheckStageId() + "_" + type + addEnd());
        List<Exploration> Data = new ArrayList<>();
        if (check(json)) {
            MyLog.L(MString.SUCCESSED + " json=" + json);
            Data.addAll(FastJsonUtil.JsonToGetExplorationList(json));
        }
        return Data;
    }

    /**
     * 指定项目踏勘点数据（新建与拆旧）*
     */
    public static List<Exploration> getExplorationList(ProjectItemBean bean, String type) {
        String json = MyApp.File.ReadFileData(addHeadPath(bean) + MString.ProjectStageExplorePointList + "_" + SetFileUtil.CheckStageId(bean) + "_" + type + addEnd());
        List<Exploration> Data = new ArrayList<>();
        if (check(json)) {
            MyLog.L(MString.SUCCESSED + " json=" + json);
            Data.addAll(FastJsonUtil.JsonToGetExplorationList(json));
        }
        return Data;
    }

    public static String getExplorationListString(String type) {
        String json = MyApp.File.ReadFileData(addHeadPath() + MString.ProjectStageExplorePointList + "_" + SetFileUtil.CheckStageId() + "_" + type + addEnd());

        return json;
    }


    /**
     * 项目踏勘点详情数据*
     */
    public static String getExplorationInfo(Exploration e) {
        // MyApp.File.SaveFileData(MyApp.currProject.getStage_id(), MString.ProjectExplorePointInfo + "_" + e.exploration_id, "", "no");//读取
        return MyApp.File.ReadFileData(addHeadExplorationInfoPath(e) + MString.ProjectExplorePointInfo + "_" + CheckExplorationId(e) + addEnd());
    }

    public static String getExplorationInfo(String e) {
        // MyApp.File.SaveFileData(MyApp.currProject.getStage_id(), MString.ProjectExplorePointInfo + "_" + e.exploration_id, "", "no");//读取
        return MyApp.File.ReadFileData(addHeadExplorationInfoPath() + MString.ProjectExplorePointInfo + "_" + e + addEnd());
    }

    /**
     * 踏勘点环境信息*
     */
    public static String getEnvInfo(Exploration e) {
        // MyApp.File.SaveFileData(MyApp.currProject.getStage_id(), MString.ProjectExplorePointInfo + "_" + e.exploration_id, "", "no");//读取
        return MyApp.File.ReadFileData(addHeadExplorationInfoPath(e) + MString.ProjectExploreEnvInfo + "_" + CheckExplorationId(e) + addEnd());
    }

    /**
     * 踏勘点跨越物上联信息*
     */
    public static String getBeforeExp(Exploration e) {
        String s = MyApp.File.ReadFileData(addHeadExplorationInfoPath(e) + MString.ProjectExploreAcrossBeforePoint + "_" + CheckExplorationId(e) + addEnd());
        MyLog.L(MString.SUCCESSED + " getBeforeExp:s=" + s);
        return s;
    }

    /**
     * 踏勘点通道特征点（无上级）*
     */
    public static String getChannelList(Exploration e) {
        String s = MyApp.File.ReadFileData(addHeadExplorationInfoPath(e) + MString.ProjectExploreAcrossOnChannelList + "_" + CheckExplorationId(e) + addEnd());
        MyLog.L(MString.SUCCESSED + " save:s=" + s);
        return s;
    }

    public static String getRequestChannelList(Exploration e) {
        String s = MyApp.File.ReadFileData(MString.Request + File.separator + addHeadExplorationInfoPath(e) + MString.ProjectExploreAcrossOnChannelList + "_" + CheckExplorationId(e) + addEnd());
        MyLog.L(MString.SUCCESSED + " save:s=" + s);
        return s;
    }

    /**
     * 踏勘点通道特征点查询*
     */
    public static String getQueryChannelFeature(PhotoPassagewayItemBean e) {
        String id = e.getExploration_id().length() == 0 ? e.getExploration_uuid() : e.getExploration_id();
        String s = MyApp.File.ReadFileData(addHeadExplorationInfoPath() + MString.ProjectExplorePointInfo + "_" + id + addEnd());
        MyLog.L(MString.SUCCESSED + " save:s=" + s);
        return s;
    }


    /**
     * 项目照片数据*
     */
    public static BackPhotosBean getExplorationPhoto() {
        BackPhotosBean bean = new BackPhotosBean();
        String path = MyApp.File.getCachaPhotoPath();
        String path2 = MyApp.File.getCachaPhotoUuidPath();
        List<BackPhoto_Bean> one = new ArrayList<>(), two = new ArrayList<>();
//        three = new ArrayList<>();
        if (!path2.equals(path)) {
            one.addAll(FileUtil.addPhotos(path2, "1"));
            two.addAll(FileUtil.addPhotos(path2, "2"));
//            three.addAll(FileUtil.addPhotos(path2, "3"));
        }
        one.addAll(FileUtil.addPhotos(path, "1"));
        two.addAll(FileUtil.addPhotos(path, "2"));
//        three.addAll(FileUtil.addPhotos(path, "3"));

        bean.getOnePhoto().setPhotoList(one);
        bean.getOnePhoto().setPhotoNum(one.size());
        bean.getTwoPhoto().setPhotoList(two);
        bean.getTwoPhoto().setPhotoNum(two.size());
//        bean.getThreePhoto().setPhotoList(three);
//        bean.getThreePhoto().setPhotoNum(three.size());
        SetFileUtil.setExplorationPhoto(bean);
        return bean;
    }

    public static String getExplorationPhotoList() {
        String s = MyApp.File.ReadFileData(addHeadPath() + MString.ProjectExplorePointInfoPhoto + "_" + CheckExplorationId(MyApp.currExploration) + addEnd());
        return s;
    }


    /**
     * 项目踏勘点详情数据*
     */
    public static String getProjectInfo() {
        // MyApp.File.SaveFileData(MyApp.currProject.getStage_id(), MString.ProjectExplorePointInfo + "_" + e.exploration_id, "", "no");//读取
        return MyApp.File.ReadFileData(addHeadPath() + MString.ProjectStageBaseInfo + "_" + SetFileUtil.CheckStageId() + addEnd());
    }

    public static String getProjectInfo(String stage) {
        // MyApp.File.SaveFileData(MyApp.currProject.getStage_id(), MString.ProjectExplorePointInfo + "_" + e.exploration_id, "", "no");//读取
        return MyApp.File.ReadFileData(MString.Project_ + stage + File.separator + MString.ProjectStageBaseInfo + "_" + stage + addEnd());
    }

    /**
     * 获取项目对象
     **/
    public static ProjectItemBean getProjectItemBeanFromCache(String stage_id) {
        String json = MyApp.File.ReadFileData(MString.Project_ + stage_id + File.separator + MString.ProjectStageBaseInfo + "_" + stage_id + addEnd());
        if (check(json)) {
            return FastJsonUtil.JsonToGetProject(json);
        }
        return null;
    }

    public static ProjectItemBean getProjectItemBeanFromRequestCache(String stage_id) {
        String json = MyApp.File.ReadFileData(MString.Request + File.separator + MString.Project_ + stage_id + File.separator + MString.ProjectStageBaseInfo + "_" + stage_id + addEnd());
        if (check(json)) {
            return FastJsonUtil.JsonToGetProject(json);
        }
        return null;
    }


    public static boolean check(String json) {
        return json != null && json.length() > 0;
    }

    /**
     * 加载项目阶段文件缓存文件夹路径头*
     */
    private static String addHeadPath() {
        return MString.Project_ + SetFileUtil.CheckStageId_type() + File.separator;
    }

    private static String addHeadPath(ProjectItemBean bean) {
        return MString.Project_ + SetFileUtil.CheckStageId_type(bean) + File.separator;
    }


    /**
     * 项目踏勘点文件缓存文件夹路径头*
     */
    private static String addHeadExplorationInfoPath(Exploration e) {
        return addHeadPath();
//        return addHeadPath() + MString.ExplorePointBelongFile + "_" + CheckExplorationId(e) + File.separator;
    }

    private static String addHeadExplorationInfoPath() {
        return addHeadPath();
//        return addHeadPath() + MString.ExplorePointBelongFile + "_" + e + File.separator;
    }


    public static String CheckExplorationId(Exploration e) {
        String s = "";
        if (e.getExploration_id().equals(MString.code_null)) {
            s = e.getUuid();
        } else {
            s = e.getExploration_id();
        }
        return s;
    }

    public static String addEnd() {
        return ".tio";
    }


}
